package com.ravi.room;

import com.ravi.common.constant.CheckExceptionEnum;
import com.ravi.common.exception.CheckException;
import com.ravi.queue.StringQueueUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class StringRoomUtil {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private StringQueueUtil stringQueueUtil;

	/**
	 * 叫号： 需要诊室队列长度>0
	 * @param roomCode 诊室编码
	 * @return 叫号人体检号
	 */
	public String callPatient(String roomCode) {
		Long length = stringQueueUtil.getQueueLength(roomCode);
		if (length > 0) {
			String headLeave = stringQueueUtil.headLeaveQueue(roomCode);
			stringRedisTemplate.opsForValue().set(roomCode + "checking", headLeave);
			return headLeave;
		} else return null;
	}

	/**
	 * 获取当前叫号
	 * @param roomCode 诊室编码
	 * @return 叫号人体检号
	 */
	public String getCheckingPatient(String roomCode) {
		String key = roomCode + "checking";
		if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(key))) {
			return stringRedisTemplate.opsForValue().get(key);
		}else return null;
	}

	/**
	 * 过号
	 * @param roomCode 诊室代码
	 * @return 过号人编码或者null
	 */
	public String missingCall(String roomCode) {
		String key = roomCode + "missingCall";
		String checkingPatient = getCheckingPatient(roomCode);
		if (StringUtils.isEmpty(checkingPatient)) {
			throw new CheckException(CheckExceptionEnum.DOC_MISSING_FAILED);
		}else {
			stringRedisTemplate.opsForList().rightPush(key, checkingPatient);
		}
		return checkingPatient;
	}

	/**
	 * 从missingCall列表中移除某个体检人员
	 * @param roomCode 诊室编码
	 * @param checkupCode 体检号
	 */
	public void removeMissingCall(String roomCode, String checkupCode) {
		String key = roomCode + "missingCall";
		stringRedisTemplate.opsForList().remove(key, 0, checkupCode);
	}

	/**
	 * 设置next_check
	 * @param roomCode 诊室编码
	 * @param nextCheck 下一诊室字符串
	 */
	public void setNextCheck(String roomCode, String nextCheck) {
		String key = roomCode + "next_check";
		stringRedisTemplate.opsForValue().set(key, nextCheck);
	}

	/**
	 * 获得next_check
	 * @param roomCode 诊室编码
	 * @return 下一诊室字符串
	 */
	public String getCurrentNextCheck(String roomCode) {
		String key = roomCode + "next_check";
		String nextCheck = stringRedisTemplate.opsForValue().get(key);
		if (StringUtils.isEmpty(nextCheck)) {
			return "";
		}else {
			return nextCheck;
		}
	}

	/**
	 * 完成检查，需要当前诊室具备患者
	 * @param roomCode 诊室编码
	 * @param nextCheck 下一诊室信息
	 */
	public void finishCheck(String roomCode, String nextCheck) {
		String checkingPatient = getCheckingPatient(roomCode);
		if (StringUtils.isEmpty(checkingPatient)) {
			throw new CheckException(CheckExceptionEnum.DOC_FINISHED_FAILED);
		}else {
			String storeKey = roomCode + "finish";
			String setKey = roomCode + "next_check";
			// 设置下一诊室信息
			setNextCheck(setKey, nextCheck);
			// 存入列表
			stringRedisTemplate.opsForList().rightPush(storeKey, nextCheck);
		}
	}

}
